﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Ude;

namespace GenerateCode.Service
{
    /// <summary>
    /// 修改项目名称
    /// </summary>
    public class RenameService
    {

        public RenameService()
        {

        }
        /// <summary>
        /// 修改项目名称
        /// </summary>
        /// <param name="name">项目新名称</param>
        public void Rename(string name)
        {
            string currentPath = Directory.GetCurrentDirectory();
            string rootPath = currentPath.Substring(0, currentPath.LastIndexOf("GenerateCode"));
            Console.WriteLine(rootPath);
           
            string[] directories = Directory.GetDirectories(rootPath);

            foreach (string directory in directories)
            {
                string directoryName = Path.GetFileName(directory);
                if (directoryName.Contains("GoodDotNet."))
                {

                    string newDirectoryName = directoryName.Replace("GoodDotNet.", name + ".");
                    string newDirectoryPath = Path.Combine(rootPath, newDirectoryName);
                    Directory.Move(directory, newDirectoryPath);
                    Console.WriteLine(newDirectoryPath);
                }
            }
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

            string project = Path.Combine(rootPath, "GoodDotNet.sln");
            if (File.Exists(project))
            {
                Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(project, name + ".sln");



                string filePath = Path.Combine(rootPath, name + ".sln"); ; // 这是你的 .sln 文件的路径

                // 读取文件内容
                string text = System.IO.File.ReadAllText(filePath);

                // 替换项目名称
                text = text.Replace("GoodDotNet", name);

                // 写入新内容
                System.IO.File.WriteAllText(filePath, text);
            }
           
            // 获取文件夹及其所有子文件夹中的所有 .cs 文件
            string[] files = System.IO.Directory.GetFiles(rootPath, "*.cs", System.IO.SearchOption.AllDirectories);

        
            foreach (string file in files)
            {

                System.Text.Encoding encoding;
                string filetext;
                // 读取文件内容，并自动检测文件的编码

                

                // 创建一个 Ude 的 CharsetDetector 实例
                CharsetDetector cdet = new CharsetDetector();
                using (FileStream fs = File.OpenRead(file))
                {
                    // 使用 CharsetDetector 检查文件的编码
                    cdet.Feed(fs);
                    cdet.DataEnd();

                    // 输出结果
                    if (cdet.Charset != null)
                    {
                        Console.WriteLine("{2}: {0}, confidence: {1}",
                            cdet.Charset, cdet.Confidence,file);
                        encoding=Encoding.GetEncoding(cdet.Charset);
                    }
                    else
                    {
                        encoding = Encoding.Default;
                        Console.WriteLine("Detection failed.");
                    }
                }
               

                using (var reader = new System.IO.StreamReader(file,encoding))
                {
                    encoding = reader.CurrentEncoding;
                    filetext = reader.ReadToEnd();

                    // 替换项目名称
                    filetext = filetext.Replace("GoodDotNet", name);


                }
                // 使用原来的编码写入新内容
                using (var writer = new System.IO.StreamWriter(file, false,encoding))
                {
                    writer.Write(filetext);
                 }
            }

            files = System.IO.Directory.GetFiles(rootPath, "*.csproj", System.IO.SearchOption.AllDirectories);

            foreach (string file in files)
            {

                System.Text.Encoding encoding;
                string filetext;
                using (var reader = new System.IO.StreamReader(file, Encoding.Default))
                {
                    encoding = reader.CurrentEncoding;
                     filetext = reader.ReadToEnd();

                    // 替换项目名称
                    filetext = filetext.Replace("GoodDotNet", name);

                }

                // 使用原来的编码写入新内容
                using (var writer = new System.IO.StreamWriter(file, false, encoding))
                {
                    writer.Write(filetext);
                }
                // 读取文件内容

                string newName = System.IO.Path.GetFileName(file).Replace("GoodDotNet", name);
                if (!newName.Equals(Path.GetFileName(file)))
                {
                    Console.WriteLine("重命名：" + file + ":" + newName);

                    Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(file, newName);
                }
            }

        }
    }
}
